package com.hejia.bulbul.scheduler;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import com.hejia.alauda.entity.Product;
import com.hejia.alauda.service.CalendarService;
import com.hejia.alauda.service.CommonService;
import com.hejia.alauda.service.PresellService;
import com.hejia.alauda.service.ProductService;
import com.hejia.alauda.service.WarningService;

/**
 * 每隔5分钟定时检查产品 提前30分钟上架【到点上架】状态的产品 根据排期预售上架产品
 * 
 * @author jatin
 *
 */
public class ProductScheduler {

	@Resource
	private ProductService productService;

	@Resource
	private CommonService commonService;

	@Resource
	private PresellService presellService;

	@Resource
	private CalendarService calendarService;

	@Resource
	private WarningService warningService;

	private static final Log Logger = LogFactory.getLog(ProductScheduler.class);

	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(cron = "0 0/1 * * * ?")
	@Async("executor")
	public void productCalculate() {

		try {
			Logger.info("开始执行到点上架产品");
			this.productService.timePublishProducts();
			Logger.info("结束执行到点上架产品");
		} catch (Exception e) {
			Logger.info("执行到点上架产品失败", e);
		}

		try {
			Logger.info("开始执行扫描排期预售产品");

			// 在售月享产品少于一个，上架产品
			this.presellService.publicPresell(Product.Category.MONTH);

			// 在售季享产品少于一个，上架产品
			this.presellService.publicPresell(Product.Category.QUARTER);

			// 钱滚翻
			this.presellService.publicPresell(Product.Category.CURRENT);

			// 上架双季汇产品
			this.presellService.publicPresell(Product.Category.HALF_A_YEAR);

			// 上架年享产品
			this.presellService.publicPresell(Product.Category.YEAR);

			// 上架新手标排期预售产品
			this.presellService.publicRookiePresell();

			Logger.info("结束执行扫描排期预售产品");
		} catch (Exception e) {
			Logger.error("执行扫描排期预售产品并插入失败", e);
		}

		try {
			warnNoProduct();
		} catch (Exception e) {
			Logger.error("执行无产品在售预警失败", e);
		}
	}

	/**
	 * 预警无产品在售，到点上架和排期预售执行完毕后执行。 部分产品预警逻辑在上架排期预售地方实现不灵活，以后移到此方法。
	 */
	private void warnNoProduct() {

		Integer countOfMonth = this.productService
				.sumOnSellProductByCategoryAndAmount(Product.Category.MONTH,
						false, 50000);
		if (countOfMonth < 1) {
			this.warningService.sendNotPresellWarning(Product.Category.MONTH);
		}

		Integer countOfQuarter = this.productService
				.sumOnSellProductByCategoryAndAmount(Product.Category.QUARTER,
						false, 50000);
		if (countOfQuarter < 1) {
			this.warningService.sendNotPresellWarning(Product.Category.QUARTER);
		}

		Integer countOfCurrent = this.productService
				.sumOnSellProductByCategoryAndAmount(Product.Category.CURRENT,
						false, 50000);
		if (countOfCurrent < 1) {
			this.warningService.sendNotPresellWarning(Product.Category.CURRENT);
		}
		Integer countOfHalfAYear = this.productService
				.sumOnSellProductByCategoryAndAmount(
						Product.Category.HALF_A_YEAR, false, 50000);
		if (countOfHalfAYear < 1) {
			this.warningService
					.sendNotPresellWarning(Product.Category.HALF_A_YEAR);
		}
		Integer countOfYear = this.productService
				.sumOnSellProductByCategoryAndAmount(Product.Category.YEAR,
						false, 50000);
		if (countOfYear < 1) {
			this.warningService.sendNotPresellWarning(Product.Category.YEAR);
		}
	}

}
